package com.tg.app.activity.account;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.base.BaseActivity;
import com.appbase.custom.base.FeedBackInfo;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;
import com.tg.app.R;
import com.tg.appcommon.android.LanguageUtils;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class HelpAndFeedbackActivity extends BaseActivity {

    LinearLayout usuallayout;
    LinearLayout typelayout;
    private String FAQ, question;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_help_feedback);
        hideActionBar();
        initView();
        FAQ = getString(R.string.application_base);
        question = getString(R.string.account_problem);
        initData();
    }

    protected void initView() {

        TextView tvTitle = findViewById(R.id.tv_help_feedback_title);
        tvTitle.setText(R.string.help_feedback);
        ImageButton back = findViewById(R.id.back_toolbar);
        back.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                finish();
            }
        });

        TextView textView = new TextView(this);
        textView.setText(R.string.faq);
        textView.setPadding(18, 18, 18, 18);
        textView.setTextColor(Color.parseColor("#838385"));

        usuallayout = findViewById(R.id.usuallayout);
        typelayout = findViewById(R.id.typelayout);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initData() {

        getDataList();
    }

    private void getDataList() {

        showLoading();

        TGHttp.getInstance().getFeedbackList(new HashMap<String, String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<Object>() {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            String content = JSON.toJSONString(object);
                            JSONObject data = new JSONObject(content);
                            final JSONArray usualarray = data.optJSONArray(FAQ);
                            final JSONObject typedata = data.optJSONObject(question);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        refreshUsual(usualarray);
                                        refreshType(typedata);
                                    } catch (Exception e) {
                                        if (LanguageUtils.isZh(HelpAndFeedbackActivity.this)) {
                                            FAQ = "FAQ";
                                            question = "Problem classification";
                                        } else {
                                            FAQ = "常见问题";
                                            question = "问题分类";
                                        }
                                        initData();
                                    } finally {
                                        //TODO
                                    }
                                }
                            });


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {

                    }

                    @Override
                    protected void onOtherError(String error) {

                    }

                    @Override
                    protected void onFinish() {
                        hideLoading();
                    }
                });
    }

    public void doSubmit(View view) {
        Intent intent = new Intent(this, FeedBackActivity.class);
        startActivity(intent);
    }


    private void refreshUsual(JSONArray array) {

        for (int i = 0; i < array.length(); i++) {
            final FeedBackInfo info = JSON.parseObject(array.optString(i), FeedBackInfo.class);
            @SuppressLint("InflateParams") RelativeLayout layout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.layout_question_item, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.setLayoutParams(params);
            TextView tv = layout.findViewById(R.id.tv);
            tv.setText(info.getTitle());
            usuallayout.addView(layout);
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(HelpAndFeedbackActivity.this, QuestionAnswerActivity.class);
                    intent.putExtra(QuestionAnswerActivity.KET_TITLE, getString(R.string.faq));
                    intent.putExtra(QuestionAnswerActivity.KEY_FEEDINFO, info);
                    startActivity(intent);
                }
            });
        }
    }

    private void refreshType(JSONObject data) {
        Iterator keys = data.keys();
        while (keys.hasNext()) {
            @SuppressLint("InflateParams") RelativeLayout layout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.layout_question_item, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.setLayoutParams(params);
            TextView tv = layout.findViewById(R.id.tv);
            final String key = keys.next().toString();
            tv.setText(key);
            typelayout.addView(layout);
            JSONArray array = data.optJSONArray(key);
            final ArrayList<FeedBackInfo> infos = new ArrayList<>();
            for (int i = 0; i < array.length(); i++) {
                infos.add(JSON.parseObject(array.optString(i), FeedBackInfo.class));
            }
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(HelpAndFeedbackActivity.this, QuestionTypeActivity.class);
                    intent.putExtra(QuestionTypeActivity.KEY_TYPE, key);
                    intent.putParcelableArrayListExtra(QuestionTypeActivity.KEY_FEEDINFOLIST, infos);
                    startActivity(intent);
                }
            });
        }
    }
}
